package com.inxedu.os.edu.controller.Images;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.inxedu.os.edu.pojo.Images.EduWebsiteImagess;
import com.inxedu.os.edu.pojo.EduWebsiteImagesType;
import com.inxedu.os.edu.pojo.Images.WebsiteImages;
import com.inxedu.os.edu.service.images.ImageTypeService;
import com.inxedu.os.edu.service.images.ImagesService;
import com.inxedu.os.edu.service.images.WebsiteImagesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
import java.util.Map;

@Controller
@Api(value = "广告图模块Controller",tags = "媒体广告图")
public class images_controller {
    @Autowired
    ImagesService imagesService;
    @Autowired
    private ImageTypeService imageTypeService;
    @Autowired
    WebsiteImagesService websiteImagesService;

    // 创建群 绑定变量名字和属性，把参数封装到类
    @InitBinder("websiteImages")
    public void initBinderWebsiteImages(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("websiteImages.");
    }

    //获取图片列表
    @RequestMapping(value = "/admin/website/imagesPage",method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    @ApiOperation(value = "获取广告图图片列表",notes = "获取广告图图片列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "websiteImages",dataType = "websiteImages.java",value = "用于展示图片列表的内容"),
            @ApiImplicitParam(name = "imagesUrl",dataType = "string",value = "跳转数据修改页面的id"),
            @ApiImplicitParam(name = "linkAddress",dataType = "string",value = "点击图片的外链"),
            @ApiImplicitParam(name = "title",dataType = "string",value = "图片标题"),
            @ApiImplicitParam(name = "seriesNumber",dataType = "int",value = "图片序列号"),
            @ApiImplicitParam(name = "typeName",dataType = "string",value = "图片类型名称")
    })
    public ModelAndView getimagelist(@RequestParam(defaultValue="1",required = false,value="page")Integer page, @ModelAttribute("websiteImages") EduWebsiteImagess websiteImages, HttpServletRequest req){
        //设置分页
        PageHelper.startPage(page,5);
        ModelAndView model = new ModelAndView();
        model.setViewName("view/inxedu/admin/website/images/websiteImages_list");
        //图片列表基本属性
        List<Map<String, Object>> maps1 = imagesService.getImageslist(websiteImages);
        PageInfo<List<Map<String, Object>>> info = new PageInfo(maps1);
        //图片类型
        List<EduWebsiteImagesType> types = imagesService.getimagesType();
        PageInfo<List<EduWebsiteImagesType>> info1 = new PageInfo(types);
        //List<Map<String, Object>> maps = imagesService.queryImagePage(eduWebsiteImages);
        model.addObject("typeList",types);
        model.addObject("websiteImagesList",maps1);
        model.addObject("page",info);
        return model;
    }
    //跳转修改页面，做数据回显
    @ResponseBody
    @RequestMapping(value = "admin/website/doUpdateImages/{id}",method = RequestMethod.GET)
    @ApiOperation(value = "跳转修改页面，数据回显",notes = "跳转修改页面，数据回显")
    @ApiImplicitParams({@ApiImplicitParam(name = "id",dataType = "int",value = "跳转数据修改页面的id")})
    public ModelAndView goupdataImages(@PathVariable int id ,HttpServletRequest request){
        ModelAndView model = new ModelAndView();
        //跳转页面
        model.setViewName("view/inxedu/admin/website/images/websiteImages_update");
        //图片列表基本属性
        EduWebsiteImagess edu = imagesService.getImagesById(id);
        //图片类型
        List<EduWebsiteImagesType> types = imagesService.getimagesType();
        model.addObject("websiteImages",edu);
        model.addObject("typeList",types);
        return model;
    }
    //图片数据更新
    @ResponseBody
    @RequestMapping(value = "/admin/website/updateImages",method = RequestMethod.POST)
    @ApiOperation(value = "广告图修改",notes ="广告图修改")
    public ModelAndView updataImages(WebsiteImages websiteImages,HttpServletRequest request){
        ModelAndView model = new ModelAndView();
        int image =websiteImagesService.toUpdataImage(websiteImages);
        model.setViewName("redirect:/admin/website/imagesPage");
       /* if(image!=0){
            //成功后跳转到列表页面
        }*/
        return model;
    }
    // 批量删除和单条删除
    @ResponseBody
    @RequestMapping(value = "admin/website/delImages",method = RequestMethod.POST)
    @ApiOperation(value = "广告图批量删除个单条删除",notes = "广告图批量删除个单条删除")
    public ModelAndView delImages(HttpServletRequest request){
        ModelAndView model = new ModelAndView();
        //获取页面图片id
        String[] ids = request.getParameterValues("imageId");
        int sum=0;
        for (int i = 0; i < ids.length ; i++) {
            //转换为int
            int id = Integer.parseInt(ids[i]);
            //执行sql
            int del = imagesService.delImages(id);
            sum++;
        }
        Object imageListUri = request.getSession().getAttribute("imageListUri");
        if(sum!=0){
            //删除sql条数不为0，成功后重定向列表
            model.setViewName("redirect:/admin/website/imagesPage");
        }
        return model;
    }
    //跳转添加广告
    @ResponseBody
    @RequestMapping(value = "/admin/website/doAddImages",method = RequestMethod.GET)
    @ApiOperation(value = "跳转添加广告",notes = "跳转添加广告")
    public ModelAndView toaddImages(){
        ModelAndView model = new ModelAndView();
        //跳转页面
        model.setViewName("view/inxedu/admin/website/images/websiteImages_add");
        List<EduWebsiteImagesType> list = imageTypeService.getAllImages();
        //图片类型回显sql
        model.addObject("typeList",list);
        return model;
    }

    /**
     * 添加图片
     * @param websiteImages
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/admin/website/addImages",method = RequestMethod.POST)
    @ApiOperation(value = "添加广告图片",notes = "添加广告图片")
    @ApiImplicitParams({@ApiImplicitParam(name = "websiteImages",dataType = "websiteImages.java",value = "用于添加图片列表的内容"),
            @ApiImplicitParam(name = "imageId",dataType = "integer",value = "图片地址的url"),
            @ApiImplicitParam(name = "imagesUrl",dataType = "string",value = "跳转数据修改页面的id"),
            @ApiImplicitParam(name = "linkAddress",dataType = "string",value = "点击图片的外链"),
            @ApiImplicitParam(name = "title",dataType = "string",value = "图片标题"),
            @ApiImplicitParam(name = "typeId",dataType = "int",value = "图片类型id"),
            @ApiImplicitParam(name = "seriesNumber",dataType = "int",value = "图片序列号"),
            @ApiImplicitParam(name = "previewUrl",dataType = "string",value = "图片缩略图地址"),
            @ApiImplicitParam(name = "color",dataType = "string",value = "背景色"),
            @ApiImplicitParam(name = "describe",dataType = "string",value = "图片描述")
    })
    public ModelAndView addImages(@ModelAttribute("websiteImages")WebsiteImages websiteImages){
        ModelAndView model = new ModelAndView();
        model.setViewName("redirect:/admin/website/imagesPage");
        //数据库imagesURL不能为空 给一个默认值
        if(websiteImages.getImagesUrl()==null && websiteImages.getImagesUrl().equals("")){
            websiteImages.setImagesUrl("https://www.baidu.com");
        }
        if(websiteImages.getLinkAddress().equals("")){
            websiteImages.setLinkAddress("https://www.mi.com");
        }
        websiteImagesService.addImages(websiteImages);
        return model;
    }

}
